<template>
    <div class="edit">
        <el-form :model="info" ref="input-form" :rules="rules"
                 class="edit-form">
            <h3 class="title">
                修改密码
            </h3>
            <el-form-item label="原密码" prop="password">
                <el-input v-model="info.password" placeholder="请输入原密码" type="password"/>
            </el-form-item>
            <el-form-item label="新密码" prop="password1">
                <el-input v-model="info.password1" placeholder="请输入新密码" type="password"/>
            </el-form-item>
            <el-form-item label="确认新密码" prop="password2">
                <el-input v-model="info.password2" placeholder="请再次输入新密码" type="password"/>
            </el-form-item>
            <el-form-item style="width:100%;">
                <div  style="text-align: center">
                <el-button @click="save()" type="primary">确 定 修 改</el-button>
                </div>
            </el-form-item>
        </el-form>
        <!--  底部  -->
    </div>
</template>

<script>
    import {mapState} from 'vuex'
    import {putPassword} from "~/admin/api/user"
    import {passwordIsRight} from '../../api/login'

    export default {
        name: "passwordEdit",
        data() {
            var validatePass1 = (rule, value, callback) => {
                var pwdRegex = new RegExp('(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*()]).{8,15}')
                if (!pwdRegex.test(value)) {
                    callback(new Error('密码长度必须不少于8位,不大于15位，并且必须同时包含数字、字母、特殊字符（特殊字符包括（英文字符）：!@#$%^&*()）'))
                }else {
                    callback()
                }
            }
            var validatePass2 = (rule, value, callback) => {
                if (this.info.password1 !== '') {
                    if (value !== this.info.password1) {
                        callback(new Error('两次输入密码不一致!'))
                    } else {
                        callback()
                    }
                } else {
                    callback()
                }
            }
            return {
                userId:'',
                username:'',
                info: {
                    password:'',
                    password1:'',
                    password2:'',
                },
                rules: {
                    password:[{required: true, message:'请输入原密码', trigger: 'blur'}],
                    password1: [
                        {required: true,min: 8, max:15, validator: validatePass1, trigger: 'change'}
                    ],
                    password2: [{required: true, validator: validatePass2, trigger: 'change'}]
                },
            }
        },
        mounted:
            function() {
                if (this.$route.query.userId!=null) {
                    this.userId=this.$route.query.userId;
                }
                if (this.$route.query.username!=null) {
                    this.username=this.$route.query.username;
                }
            },

        methods: {
            save() {
                this.$refs['input-form'].validate(valid => {
                    if (valid) {
                        passwordIsRight(this.username,this.info.password).then(response=>{
                            if (response.data.code==0){
                                let param = {
                                    userId:this.userId,
                                    newpassword1:this.info.password1,
                                    username:this.username
                                }
                                putPassword(param).then(resp=>{
                                    if(resp.status === 200){
                                        this.$message.success('修改成功');
                                        this.$router.push('/login')
                                    }
                                })
                            }else {
                                alert('原密码错误')
                            }
                        })
                    }
                })
            },
        }
    }
</script>
<style scoped lang="scss">
    

    .edit {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100vw;
        height: 100vh;
        background-size: cover;
        background-color: $bg-color;
    }

    .title {
        margin: 0 auto 30px auto;
        text-align: center;
        color: #707070;
    }

    .edit-form {
        border-radius: 6px;
        background: #ffffff;
        width: 385px;
        padding: 25px 25px 5px 25px;

        .el-input {
            height: 38px;

            input {
                height: 38px;
            }
        }

        .input-icon {
            height: 39px;
            width: 14px;
            margin-left: 2px;
        }
    }
</style>
